package com.dean.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 根据模板解析并填充内容
 * 不支持数据量较大的填充
 */
public class DeanExcelParseUtils {
    /**
     *  参数解析入口
     * @param param
     * @param dataset
     * @return
     */
    public static Object deanParse(String param, Map<String, Object> dataset){
        String [] params = param.split("\\.");
        // 多参数含集合处理
        Map<String,Object> newset = dataset;
        for (int i = 0; i < params.length-1; i++) {
            String val = params[i];
            int begin = val.indexOf("[");
            int end   = val.indexOf("]");
            // 不含集合
            if(begin == -1){
                newset = mapParse(params[i],newset);
                continue;
            }
            String param1 = val.substring(0,begin);
            while(begin>-1 && end > -1){
                Integer param2 = Integer.parseInt(val.substring(begin+1,end));
                List<Map<String,Object>> list = listParse(param1,newset);
                if(list == null || list.isEmpty()){
                    break;
                }
                newset = list.get(param2);
                begin = val.indexOf("[",end);
                end = val.indexOf("[",end);
            }
        }
        return objParse(params[params.length-1],newset);
    }
    // 直接取值的
    public static Object objParse(String param,Map<String,Object> dataset){
        return dataset.get(param);
    }
    // 取map对象
    public static Map<String,Object> mapParse(String param,Map<String,Object> dataset){
        return (Map<String,Object>)dataset.get(param);
    }

    public static List<Map<String,Object>> listParse(String param,Map<String,Object> dataset){
        return (List<Map<String,Object>>)dataset.get(param);
    }

}
